<template>
  <div>
    <div v-if="showMenuBar" class="menu-bar">
      <h1 class="logo" @click="goToHome">云萌工作室</h1>
      <nav class="navbar">
        <button @click="refreshPage" class="refresh-button">
          ⟳
        </button>
  <button @click="toggleFullscreen" class="fullscreen-button">
    ⛶
  </button>
</nav>
<ul class="user-info">
  <li>
    <img src="/CC_C.jpg" alt="登录" @click="toggleDropdown" class="login-icon">
    <span class="loguser">欢迎 {{ currentUsername }}</span>
   <div class="dropdown-menu">
      <ul class="center">
        <li><a href="https://home.cloudcode.team/">技术员中心</a></li>
        <li><router-link to="#" @click="logout">退出登录</router-link></li>
      </ul>
    </div>

  </li>
</ul>
    </div>
    <div id="app">
      <!-- 条件渲染 Sidebar 和菜单按钮 -->
      <Sidebar v-if="showSidebar" id="sidebar" />
      <div class="content" :class="{ shifted: showSidebar }" id="content">
        <div class="menu-button-container" v-if="showSidebar" id="menu-button-container">
          <button class="menu-button" @click="toggleMenu">☰ 菜单</button>
        </div>
        <router-view />
      </div>
    </div>
  </div>
<p>created</p>
</template>

<script>
import { ref, computed, onMounted, onUnmounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import Sidebar from './components/Sidebar.vue';
export default {
  components: {
    Sidebar,
  },

  setup() {
    const route = useRoute();
    const router = useRouter();

    const currentUsername = ref('未登录'); // 初始用户名
    const sidebarShifted = ref(false); // 用于控制侧边栏和 logo 的移动
    const refreshPage = () => {
      location.reload(); // 刷新页面
    };
    const toggleFullscreen = () => {
    if (!document.fullscreenElement) {
      document.documentElement.requestFullscreen().catch(err => {
        console.log(`Error attempting to enable full-screen mode: ${err.message} (${err.name})`);
      });
    } else {
      document.exitFullscreen();
    }
  };//放大页面

    onMounted(() => {
      // 从 localStorage 中读取用户名
      const storedUsername = localStorage.getItem('username');
      if (storedUsername) {
        currentUsername.value = storedUsername;
      }
      
      // 处理点击事件监听
      document.addEventListener('click', handleClickOutside);
    });

    onUnmounted(() => {
      // 移除点击事件监听
      document.removeEventListener('click', handleClickOutside);
    });

    const showSidebar = computed(() => {
      return !['/', '/register', '/login'].includes(route.path);
    });

    const showMenuBar = computed(() => {
      return !['/', '/register', '/login'].includes(route.path);
    });

    const toggleMenu = () => {
      const sidebar = document.getElementById('sidebar');
      const content = document.getElementById('content');
      if (sidebar && content) {
        if (sidebar.style.transform === 'translateX(-10px)') {
          sidebar.style.transform = 'translateX(-250px)';
          content.classList.remove('shifted');
        } else {
          sidebar.style.transform = 'translateX(-10px)';
          content.classList.add('shifted');
        }
      }
    };

    const preventDefault = (event) => {
      event.preventDefault();
    };

    const deleteAllCookies = () => {
      const cookies = document.cookie.split(";");
      for (const cookie of cookies) {
        const eqPos = cookie.indexOf("=");
        const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/";
      }
    };

    const logout = (event) => {
      event.preventDefault();
      // 删除本地存储
      localStorage.clear();
      sessionStorage.clear();
      // 删除所有cookie
      deleteAllCookies();
      // 跳转到登录页面
      router.push('/login'); 
    };

    const goToHome = () => {
      window.location.href = 'https://home.cloudcode.team/';
    };

    const handleClickOutside = (event) => {
      const sidebar = document.getElementById('sidebar');
      const content = document.getElementById('content');
      const menuButton = document.querySelector('.menu-button');

      if (!sidebar || !content || !menuButton) {
        return; // 检查元素是否为 null
      }

      const isClickInsideSidebar = sidebar.contains(event.target);
      const isClickInsideButton = menuButton.contains(event.target);

      if (!isClickInsideSidebar && !isClickInsideButton) {
        sidebar.style.transform = 'translateX(-250px)';
        content.classList.remove('shifted');
        sidebarShifted.value = false; // 重置 logo 位置
      }
    };

    return {
      currentUsername, // 返回用户名
      showSidebar,
      showMenuBar,
      toggleMenu,
      preventDefault,
      logout,
      goToHome,
      refreshPage,
      toggleFullscreen,
    };
  }
};
</script>

<style scoped>
.loguser {
margin-right: 80px;
 color: #ffffff;
 font-size: large;
 z-index: 1000;
 font-family: 楷体;
 margin-left: 10px;
 position: relative; /* 添加相对定位 */
 top: -15px;
}

:root {
  --color-primary: #0073ff;
  --color-white: #e9e9e9;
  --color-black: #141d28;
  --color-black-1: #212b38;
}
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  font-family: sans-serif;
}
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  text-align: center;
  color: #2c3e50;
  display: flex;
  height: 100vh;
  background-image: linear-gradient(135deg, rgba(251, 247, 247, 0.5), rgba(187, 222, 251, 0.5)),
    url(../public/img/R.png);
}
.center {
  text-emphasis-color: var(--color-white);
}
.content {
  flex: 1;
  transition: margin-left 0.3s ease;
}
.content.shifted {
  transition: margin-left 0.3s ease;
  margin-left: 250px;
}
.menu-button-container {
  padding: 10px;
  text-align: left;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 0;
}
.menu-button {
  background-color: #42b983;
  border: none;
  color: white;
  padding: 10px;
  font-size: 16px;
  cursor: pointer;
  margin-left: -10px;
}
.menu-button:hover {
  background-color: #333;
}
.menu-button:focus {
  outline: none;
}
.sidebar {
  width: 250px;
  position: fixed;
  top: 0;
  left: 0;
  height: 100%;
  background-color: #333;
  overflow-x: hidden;
  transition: transform 0.3s ease;
  transform: translateX(-250px);
  z-index: 1;
}
.sidebar a {
  padding: 10px 15px;
  text-decoration: none;
  font-size: 18px;
  color: white;
  display: block;
  transition: background-color 0.3s;
}
.sidebar a:hover {
  background-color: #575757;
}
.menu-bar {
  background-color: #333;
  height: 80px;
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 5%;
  position: relative;
  top: -10px;
}
/* 文字logo */
.logo {
  text-align: center;
  background: linear-gradient(90deg, #ff8800, #01fb33); /* 渐变色 */
  -webkit-background-clip: text; 
  -webkit-text-fill-color: transparent; 
  font-size: 35px;
  margin-left: 200px;
  font-family: 楷体;
  transition: transform 0.3s ease; /* 添加过渡效果 */
}
.dropdown-menu {
  display: none;
  z-index: 1000;
}
.menu-bar ul li:hover .dropdown-menu {
  display: block;
  position: absolute;
  left: 0;
  top: 100%;
  background-color: var(--color-black);
}
.menu-bar ul li:hover .dropdown-menu ul {
  display: block;
  margin: 10px;
}
.menu-bar ul li:hover .dropdown-menu ul li {
  width: 150px;
  padding: 10px;
}
.menu-bar ul {
  list-style: none;
  display: flex;
}
.menu-bar ul li {
  padding: 10px 30px;
  position: relative;
}
.menu-bar ul li a {
  color: white;
  font-size: 20px;
  text-decoration: none;
  transition: all 0.3s;
}
.menu-bar ul li a:hover {
  color: var(--color-primary);
}
/* 刷新 */
.refresh-button {
  background: none;
  border: none;
  color: white;
  font-size: 25px;
  cursor: pointer;
  position: absolute;
  right: 410px; 
  top: 53%; 
  transform: translateY(-50%);
}
/* 放大 */
.fullscreen-button {
  background: none;
  border: none;
  color: white;
  font-size: 24px;
  cursor: pointer;
  position: absolute;
  right: 380px; /* 与用户名相隔50px */
  top: 50%; 
  transform: translateY(-50%);
}
.login-icon {
  cursor: pointer;
  width: 55px;
  margin-right: 10px; /* 与用户名的间距 */
}


</style>
